import MusicAPI from '@/services/ant-design-pro/music'
import ModelTool from './BaseTool'
const { getSingerList, getLyricAuthorList, getMelodyAuthorList, getMusicAlbumList, getClassifyList, deleteClassify } = MusicAPI
const { setSelectParams, resetTags } = ModelTool;

export default {
    namespace: 'music',
    state: {
        SingerSelect: [],
        LyricAuthorSelect: [],
        MelodyAuthorSelect: [],
        AlbumSelect: [],
        classifyTags: [],
        ResetTags: [],
    },
    effects: {
        *getClassifyTags({ payload, callback = null }, { call, put, select }) {
            const Tags = yield select(state => state.music.classifyTags);
            let result = Tags;
            if (Tags.length <= 0 || payload && payload.refresh === true) {
                result = yield call(getClassifyList);
                result = result.data.value;

                yield put({
                    type: 'reducer',
                    ResetTags: yield call(resetTags, result),
                    classifyTags: result,
                })
            }

            callback && callback(result);
        },
        /**
         * 刷新选项
         * @param {String} payload._key (选择字段) 要求大写开头 (Singer,LyricAuthor,MelodyAuthor,Album)
         * @param {Boolean} payload.refresh (是否强刷)
         */
        *refreshSelect({ payload, callback }, { call, put, select }) {
            let _key = payload.key;
            const Select = yield select(state => state.music[`${_key}Select`]);
            let result = Select;
            if (Select.length <= 0 || payload.refresh === true) {
                result = yield call(MusicAPI[`get${_key}List`], {
                    isAll: true
                });
                result = yield call(setSelectParams, _key, result.data.value);

                yield put({
                    type: 'setSelect',
                    payload: result,
                    _key
                })
            }
        }
    },
    reducers: {
        setSelect(state, action) {
            let result = { ...state };
            result[`${action._key}Select`] = action.payload;
            return result;
        },
        reducer(state, action) {
            return {
                ...state,
                ...action
            }
        }
    }
}